package com.auto.api.core.type.classtomap;

import com.auto.api.core.entity.ClassToMapContext;
import com.auto.api.core.entity.ProcessProp;
import com.auto.api.core.entity.PropertyContext;
import com.auto.api.core.type.TypeFactory;
import com.auto.api.core.type.defaultvalue.ValueTypeHandler;
import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program:
 * @description: 类型转换接口
 * @author: liangshf
 * @create: 2020-09-24
 */
public class ClassToResultColumnMapHandler extends AbstractClassToMapHandler {

    @Override
    public ProcessProp<ClassToMapContext> getProcessProp(ClassToMapContext context, PropertyContext propertyContext) {
        return new ProcessProp(propertyContext.getJudgePropType(), context, propertyContext);
    }

    @Override
    public void processProp(ProcessProp<ClassToMapContext> processProp) {
        PropertyDescriptor prop = processProp.getPropertyContext().getProp();
        //多个继承泛型替换
        ValueTypeHandler typeHandler = TypeFactory.getTypeHandler(processProp.getPropType());
        if (typeHandler != null) {
            if (processProp.getPropertyContext().isCollectionType()) {
                //默认使用集合
                buildList(processProp.getContext(), prop, typeHandler.getTypeDefaultValue());
                return;
            }
            processProp.getContext().getResult().put(prop.getName(), typeHandler.getTypeDefaultValue());
        } else {
            Map<String, Object> columnMap = getResult(new ClassToMapContext(processProp.getPropertyContext().isObjParse() ? processProp.getPropertyContext().getGenPropResType() : processProp.getPropertyContext().getPropResType()));
            if (processProp.getPropertyContext().isCollectionType()) {
                buildList(processProp.getContext(), prop, columnMap);
                return;
            }
            processProp.getContext().getResult().put(prop.getName(), columnMap);
        }
    }

    private void buildList(ClassToMapContext classToMapContext, PropertyDescriptor prop, Object typeDefaultValue) {
        List<Object> list = new ArrayList();
        list.add(typeDefaultValue);
        classToMapContext.getResult().put(prop.getName(), list);
    }
}
